﻿using WeifenLuo.WinFormsUI.Docking;
using System.IO;
using System.Windows.Forms;

namespace GameDesigner2
{
    public partial class ConsoleForm : DockContent
    {
        public class ConsoleWriter : TextWriter 
        {
            private TextWriter _Writer;
            private TextBox _TextBox;

            public ConsoleWriter(TextWriter writer, TextBox text)
            {
                _Writer = writer;
                _TextBox = text;
            }

            public override void Write(char value)
            {
                base.Write(value);
                _TextBox.Text = _TextBox.Text + value;
                if (value == '\n' ) 
                {
                    _TextBox.SelectionStart = _TextBox.TextLength;
                    _TextBox.ScrollToCaret();
                }
            }

            public override System.Text.Encoding Encoding
            {
                get { return _Writer.Encoding; }
            }
        }

        public ConsoleForm()
        {
            InitializeComponent();
            ConsoleWriter writer = new ConsoleWriter(System.Console.Out, _TextBox1);

            System.Console.SetOut(writer);
        }

        void ToolStripButton1Click(object sender, System.EventArgs e)
        {
            _TextBox1.Clear();
        }
        
    }
}
